/*     */ package com.zimbra.qa.unittest.prov.soap;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*     */ import com.zimbra.cs.account.SearchDirectoryOptions.SortOpt;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.RightModifier;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.qa.QA.Bug;
/*     */ import com.zimbra.qa.unittest.prov.Verify;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Ignore;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSearchDirectory
/*     */   extends SoapTest
/*     */ {
/*     */   private static final String PASSWORD = "test123";
/*     */   private static SoapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  66 */     provUtil = new SoapProvTestUtil();
/*  67 */     prov = provUtil.getProv();
/*  68 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  73 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private static void createAccounts(String domainName, int numAccounts)
/*     */     throws Exception
/*     */   {
/*  79 */     for (int i = 0; i < numAccounts; i++) {
/*  80 */       String acctName = "user-" + (i + 1) + "@" + domainName;
/*     */       
/*  82 */       Account acct = provUtil.createAccount(acctName, "test123");
/*     */       
/*  84 */       if ((i + 1) % 100 == 0) {
/*  85 */         System.out.println("Created " + (i + 1) + " accounts");
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private String expandFilter(String filter, String key)
/*     */   {
/*  95 */     Map<String, String> vars = new HashMap();
/*  96 */     vars.put("n", key);
/*  97 */     return LdapUtil.expandStr(filter, vars);
/*     */   }
/*     */   
/*     */   private List searchDirectory(String key, boolean expectTooMany) throws Exception {
/* 101 */     String FILTER_1 = "(|(uid=*%n*)(cn=*%n*)(sn=*%n*)(gn=*%n*)(displayName=*%n*)(zimbraId=%n)(mail=*%n*)(zimbraMailAlias=*%n*)(zimbraMailDeliveryAddress=*%n*)(zimbraDomainName=*%n*))";
/* 102 */     String[] ATTRS = { "displayName", "zimbraId", "zimbraMailHost", "uid", "zimbraAccountStatus", "zimbraLastLogonTimestamp", "description", "zimbraMailStatus", "zimbraCalResType", "zimbraDomainType", "zimbraDomainName" };
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 116 */     String query = expandFilter("(|(uid=*%n*)(cn=*%n*)(sn=*%n*)(gn=*%n*)(displayName=*%n*)(zimbraId=%n)(mail=*%n*)(zimbraMailAlias=*%n*)(zimbraMailDeliveryAddress=*%n*)(zimbraDomainName=*%n*))", key);
/*     */     
/* 118 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 119 */     options.setDomain(null);
/* 120 */     options.setMaxResults(5000);
/* 121 */     options.setFilterString(null, query);
/* 122 */     options.setReturnAttrs(ATTRS);
/* 123 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/* 124 */     options.setSortAttr("name");
/* 125 */     options.setConvertIDNToAscii(true);
/*     */     
/* 127 */     List results = null;
/* 128 */     boolean good = false;
/*     */     try {
/* 130 */       results = prov.searchDirectory(options);
/* 131 */       good = true;
/*     */     } catch (AccountServiceException e) {
/* 133 */       if ((expectTooMany) && (e.getCode().equals("account.TOO_MANY_SEARCH_RESULTS")))
/*     */       {
/* 135 */         good = true;
/*     */       }
/*     */     } catch (Exception e) {
/* 138 */       e.printStackTrace();
/*     */     }
/*     */     
/* 141 */     Assert.assertTrue(good);
/* 142 */     return results;
/*     */   }
/*     */   
/*     */   @Test
/*     */   @Ignore
/*     */   @QA.Bug(bug={24168})
/*     */   public void bug24168() throws Exception {
/* 149 */     int NUM_ACCTS = 1000;
/*     */     
/*     */ 
/* 152 */     String DOMAIN_NAME = ("bug24168." + domain.getName()).toLowerCase();
/* 153 */     Domain domain = provUtil.createDomain(DOMAIN_NAME);
/* 154 */     createAccounts(DOMAIN_NAME, 1000);
/*     */     
/* 156 */     System.out.flush();
/* 157 */     for (int i = 0; i < 10; i++) {
/* 158 */       System.out.flush();
/* 159 */       System.out.println("iteration " + (i + 1));
/* 160 */       System.out.flush();
/*     */       
/* 162 */       System.out.println("search all");System.out.flush();
/* 163 */       List results = searchDirectory("user", true);
/*     */       
/* 165 */       System.out.println("search one");System.out.flush();
/* 166 */       results = searchDirectory("user-1000", false);
/* 167 */       Assert.assertEquals(1L, results.size());
/* 168 */       Account acct = (Account)results.get(0);
/* 169 */       Assert.assertEquals("user-1000", acct.getName().substring(0, 9));
/*     */     }
/*     */   }
/*     */   
/*     */   private static class Bug39514 {
/* 174 */     private static String BUG_39514_TEST_DOAMIN = "bug39514-test-";
/*     */     
/*     */     private static final int numAcctsInDomain1 = 5;
/*     */     
/*     */     private static final int numAcctsInDomain2 = 10;
/*     */     private static final int numAcctsInDomain3 = 3;
/* 180 */     private static boolean verbose = false;
/*     */     
/*     */     private static class TestData {
/*     */       String mAdminName;
/*     */       String mAdminPassword;
/*     */       Integer mExpectedNumEntriesFound;
/*     */       
/*     */       TestData(String adminName, String adminPassword, Integer expectedNumEntriesFound) {
/* 188 */         this.mAdminName = adminName;
/* 189 */         this.mAdminPassword = adminPassword;
/* 190 */         this.mExpectedNumEntriesFound = expectedNumEntriesFound;
/*     */       }
/*     */     }
/*     */     
/*     */     private static List<TestData> setup() throws Exception
/*     */     {
/* 196 */       String DOMAIN_NAME = ("Bug39514." + TestSearchDirectory.domain.getName()).toLowerCase();
/*     */       
/* 198 */       String domain1Name = BUG_39514_TEST_DOAMIN + "1." + DOMAIN_NAME;
/* 199 */       String domain2Name = BUG_39514_TEST_DOAMIN + "2." + DOMAIN_NAME;
/* 200 */       String domain3Name = BUG_39514_TEST_DOAMIN + "3." + DOMAIN_NAME;
/*     */       
/* 202 */       String adminAcct11Name = "admin-1-1@" + domain1Name;
/* 203 */       String adminAcct12Name = "admin-1-2@" + domain1Name;
/* 204 */       String adminAcct21Name = "admin-2-1@" + domain2Name;
/* 205 */       String adminAcct22Name = "admin-2-2@" + domain2Name;
/* 206 */       String globalAdminName = "admin-global@" + domain3Name;
/*     */       
/*     */ 
/* 209 */       TestData test11 = new TestData(adminAcct11Name, "test123", Integer.valueOf(19));
/*     */       
/* 211 */       TestData test12 = new TestData(adminAcct12Name, "test123", Integer.valueOf(7));
/*     */       
/* 213 */       TestData test21 = new TestData(adminAcct21Name, "test123", Integer.valueOf(19));
/*     */       
/* 215 */       TestData test22 = new TestData(adminAcct22Name, "test123", Integer.valueOf(12));
/*     */       
/* 217 */       TestData globalAdmin = new TestData(globalAdminName, "test123", Integer.valueOf(23));
/*     */       
/*     */ 
/* 220 */       List<TestData> tests = new ArrayList();
/* 221 */       tests.add(test11);
/* 222 */       tests.add(test12);
/* 223 */       tests.add(test21);
/* 224 */       tests.add(test22);
/* 225 */       tests.add(globalAdmin);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 230 */       Domain domain1 = TestSearchDirectory.provUtil.createDomain(domain1Name);
/* 231 */       Domain domain2 = TestSearchDirectory.provUtil.createDomain(domain2Name);
/* 232 */       Domain domain3 = TestSearchDirectory.provUtil.createDomain(domain3Name);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 237 */       TestSearchDirectory.createAccounts(domain1Name, 5);
/* 238 */       TestSearchDirectory.createAccounts(domain2Name, 10);
/* 239 */       TestSearchDirectory.createAccounts(domain3Name, 3);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 244 */       Map<String, Object> adminGroup1Attrs = new HashMap();
/* 245 */       adminGroup1Attrs.put("zimbraIsAdminGroup", "TRUE");
/* 246 */       DistributionList adminGroup1 = TestSearchDirectory.prov.createDistributionList("adminGroup-1@" + domain1Name, adminGroup1Attrs);
/*     */       
/* 248 */       Map<String, Object> adminGroup2Attrs = new HashMap();
/* 249 */       adminGroup2Attrs.put("zimbraIsAdminGroup", "TRUE");
/* 250 */       DistributionList adminGroup2 = TestSearchDirectory.prov.createDistributionList("adminGroup-2@" + domain2Name, adminGroup2Attrs);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 256 */       String right = "domainAdminConsoleRights";
/* 257 */       TestSearchDirectory.prov.grantRight(TargetType.domain.getCode(), TargetBy.id, domain1.getId(), GranteeType.GT_GROUP.getCode(), GranteeSelector.GranteeBy.id, adminGroup1.getId(), null, right, null);
/*     */       
/*     */ 
/*     */ 
/* 261 */       TestSearchDirectory.prov.grantRight(TargetType.domain.getCode(), TargetBy.id, domain2.getId(), GranteeType.GT_GROUP.getCode(), GranteeSelector.GranteeBy.id, adminGroup2.getId(), null, right, null);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 269 */       Account adminAcct11 = TestSearchDirectory.provUtil.createDelegatedAdmin(adminAcct11Name, "test123");
/* 270 */       Account adminAcct12 = TestSearchDirectory.provUtil.createDelegatedAdmin(adminAcct12Name, "test123");
/* 271 */       Account adminAcct21 = TestSearchDirectory.provUtil.createDelegatedAdmin(adminAcct21Name, "test123");
/* 272 */       Account adminAcct22 = TestSearchDirectory.provUtil.createDelegatedAdmin(adminAcct22Name, "test123");
/* 273 */       Account globalAdminAcct = TestSearchDirectory.provUtil.createGlobalAdmin(globalAdminName, "test123");
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 278 */       TestSearchDirectory.prov.addMembers(adminGroup1, new String[] { adminAcct11Name, adminAcct12Name });
/* 279 */       TestSearchDirectory.prov.addMembers(adminGroup2, new String[] { adminAcct21Name, adminAcct22Name });
/*     */       
/* 281 */       TestSearchDirectory.prov.addMembers(adminGroup1, new String[] { adminAcct21Name });
/* 282 */       TestSearchDirectory.prov.addMembers(adminGroup2, new String[] { adminAcct11Name });
/*     */       
/* 284 */       return tests;
/*     */     }
/*     */     
/*     */     private static void doTest(TestData testData) throws Exception {
/* 288 */       String adminName = testData.mAdminName;
/* 289 */       String adminPassword = testData.mAdminPassword;
/* 290 */       int expectedNumEntries = testData.mExpectedNumEntriesFound.intValue();
/*     */       
/* 292 */       System.out.println("\n\n=========================");
/* 293 */       System.out.println("Testing as " + adminName);
/*     */       
/* 295 */       SoapProvisioning prov = SoapProvTestUtil.getSoapProvisioning(adminName, adminPassword);
/*     */       
/* 297 */       String[] attrs = { "displayName", "zimbraId", "zimbraMailHost", "uid", "zimbraCOSId", "zimbraAccountStatus", "zimbraLastLogonTimestamp", "description", "zimbraIsDelegatedAdminAccount", "zimbraIsAdminAccount", "zimbraMailStatus", "zimbraIsAdminGroup", "zimbraCalResType", "zimbraDomainType", "zimbraDomainName", "zimbraIsDelegatedAdminAccount", "zimbraIsAdminGroup" };
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 316 */       SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 317 */       options.setConvertIDNToAscii(true);
/* 318 */       options.setDomain(null);
/* 319 */       options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.aliases });
/* 320 */       options.setMaxResults(5000);
/* 321 */       options.setFilterString(null, null);
/* 322 */       options.setReturnAttrs(attrs);
/* 323 */       options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/* 324 */       options.setSortAttr("name");
/*     */       
/* 326 */       List<NamedEntry> results = null;
/* 327 */       long startTime = System.currentTimeMillis();
/*     */       try {
/* 329 */         results = prov.searchDirectory(options); } catch (Exception e) { long endTime;
/*     */         long elapsedTime;
/* 331 */         Iterator i$; NamedEntry entry; List<NamedEntry> verifyResults; Iterator i$; NamedEntry entry; e.printStackTrace(); } finally { long endTime;
/*     */         long elapsedTime;
/* 333 */         Iterator i$; NamedEntry entry; List<NamedEntry> verifyResults; Iterator i$; NamedEntry entry; long endTime = System.currentTimeMillis();
/* 334 */         long elapsedTime = endTime - startTime;
/* 335 */         System.out.println("Finished, elapsedTime=" + elapsedTime + " milli seconds");
/*     */         
/* 337 */         if (results != null) {
/* 338 */           System.out.println("Found " + results.size() + " entries");
/*     */           
/* 340 */           if (verbose) {
/* 341 */             System.out.println();
/* 342 */             for (NamedEntry entry : results) {
/* 343 */               System.out.println(entry.getName());
/*     */             }
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 354 */         List<NamedEntry> verifyResults = Lists.newArrayList();
/* 355 */         for (NamedEntry entry : results) {
/* 356 */           if (entry.getName().contains(BUG_39514_TEST_DOAMIN)) {
/* 357 */             verifyResults.add(entry);
/*     */           }
/*     */         }
/*     */         
/* 361 */         Assert.assertEquals(expectedNumEntries, verifyResults.size());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   @QA.Bug(bug={39514})
/*     */   public void bug39514() throws Exception
/*     */   {
/* 370 */     List<TestSearchDirectory.Bug39514.TestData> tests = Bug39514.access$400();
/*     */     
/* 372 */     for (TestSearchDirectory.Bug39514.TestData test : tests) {
/* 373 */       Bug39514.doTest(test);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class Bug40499
/*     */   {
/* 379 */     private static String BUG_40499_TEST_DOAMIN = "bug40499-test-";
/*     */     
/*     */     private static class TestData {
/*     */       String mAdminName;
/*     */       String mAdminPassword;
/*     */       Set<String> mExpected;
/*     */       
/*     */       private TestData(String adminName, String adminPassword, String[] expected) {
/* 387 */         this.mAdminName = adminName;
/* 388 */         this.mAdminPassword = adminPassword;
/* 389 */         this.mExpected = new HashSet(Arrays.asList(expected));
/*     */       }
/*     */     }
/*     */     
/*     */     private static List<TestData> setup() throws Exception {
/* 394 */       String DOMAIN_NAME = ("Bug40499." + TestSearchDirectory.domain.getName()).toLowerCase();
/*     */       
/*     */ 
/* 397 */       String denyDomainName = BUG_40499_TEST_DOAMIN + "deny." + DOMAIN_NAME;
/* 398 */       String allowDomainName = BUG_40499_TEST_DOAMIN + "allow." + DOMAIN_NAME;
/*     */       
/* 400 */       Domain denyDomain = TestSearchDirectory.prov.createDomain(denyDomainName, new HashMap());
/* 401 */       Domain allowDomain = TestSearchDirectory.prov.createDomain(allowDomainName, new HashMap());
/*     */       
/*     */ 
/* 404 */       String denyAdminName = "da@" + denyDomainName;
/* 405 */       String allowAdminName = "da@" + allowDomainName;
/*     */       
/* 407 */       Account denyAdmin = TestSearchDirectory.provUtil.createDelegatedAdmin(denyAdminName, "test123");
/* 408 */       Account allowAdmin = TestSearchDirectory.provUtil.createDelegatedAdmin(allowAdminName, "test123");
/*     */       
/*     */ 
/* 411 */       String right = "adminConsoleDomainRights";
/* 412 */       TestSearchDirectory.prov.grantRight(TargetType.global.getCode(), null, null, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, denyAdmin.getId(), null, right, null);
/*     */       
/*     */ 
/* 415 */       TestSearchDirectory.prov.grantRight(TargetType.global.getCode(), null, null, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, allowAdmin.getId(), null, right, null);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 420 */       TestSearchDirectory.prov.grantRight(TargetType.domain.getCode(), TargetBy.id, denyDomain.getId(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.id, denyAdmin.getId(), null, right, RightModifier.RM_DENY);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 425 */       TestData test1 = new TestData(denyAdminName, "test123", new String[] { allowDomainName }, null);
/* 426 */       TestData test2 = new TestData(allowAdminName, "test123", new String[] { allowDomainName, denyDomainName }, null);
/*     */       
/* 428 */       List<TestData> tests = new ArrayList();
/* 429 */       tests.add(test1);
/* 430 */       tests.add(test2);
/*     */       
/* 432 */       return tests;
/*     */     }
/*     */     
/*     */     private static void doTest(TestData testData) throws Exception
/*     */     {
/* 437 */       System.out.println("\n\n=========================");
/* 438 */       System.out.println("Testing as " + testData.mAdminName);
/*     */       
/* 440 */       SoapProvisioning prov = SoapProvTestUtil.getSoapProvisioning(testData.mAdminName, testData.mAdminPassword);
/*     */       
/*     */ 
/* 443 */       String[] attrs = { "zimbraId", "zimbraDomainname" };
/*     */       
/*     */ 
/*     */ 
/* 447 */       SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 448 */       options.setConvertIDNToAscii(true);
/* 449 */       options.setDomain(null);
/* 450 */       options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.domains });
/* 451 */       options.setMaxResults(5000);
/* 452 */       options.setFilterString(null, null);
/* 453 */       options.setReturnAttrs(attrs);
/* 454 */       options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/* 455 */       options.setSortAttr("name");
/*     */       
/* 457 */       List<NamedEntry> results = null;
/* 458 */       long startTime = System.currentTimeMillis();
/*     */       try {
/* 460 */         results = prov.searchDirectory(options); } catch (Exception e) { long endTime;
/*     */         long elapsedTime;
/* 462 */         Iterator i$; NamedEntry entry; List<NamedEntry> verifyResults; Iterator i$; NamedEntry entry; e.printStackTrace(); } finally { long endTime;
/*     */         long elapsedTime;
/* 464 */         Iterator i$; NamedEntry entry; List<NamedEntry> verifyResults; Iterator i$; NamedEntry entry; long endTime = System.currentTimeMillis();
/* 465 */         long elapsedTime = endTime - startTime;
/* 466 */         System.out.println("Finished, elapsedTime=" + elapsedTime + " milli seconds");
/*     */         
/* 468 */         if (results != null) {
/* 469 */           System.out.println("Found " + results.size() + " entries");
/*     */           
/* 471 */           System.out.println();
/* 472 */           for (NamedEntry entry : results) {
/* 473 */             System.out.println(entry.getName());
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 478 */         List<NamedEntry> verifyResults = Lists.newArrayList();
/* 479 */         for (NamedEntry entry : results) {
/* 480 */           if (entry.getName().contains(BUG_40499_TEST_DOAMIN)) {
/* 481 */             verifyResults.add(entry);
/*     */           }
/*     */         }
/*     */         
/* 485 */         Verify.verifyEquals(testData.mExpected, verifyResults);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   @QA.Bug(bug={40499})
/*     */   public void bug40499() throws Exception
/*     */   {
/* 494 */     List<TestSearchDirectory.Bug40499.TestData> tests = Bug40499.access$700();
/*     */     
/* 496 */     for (TestSearchDirectory.Bug40499.TestData test : tests) {
/* 497 */       Bug40499.doTest(test);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/soap/TestSearchDirectory.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */